<!--租户列表给管理员看-->
<template>
  <div>
    <!--卡片视图区域-->
    <el-card class="box-card">
      <!--搜索与添加区域-->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="gettenantList()">
            <el-button slot="append" icon="el-icon-search" @click="gettenantList()"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!--用户列表区域-->
      <el-table :data="showtenantList" highlight-current-row
                :row-class-name="tableRowClassName" @row-click = "onRowClick" border stripe>
        <el-table-column label="" type="index">
        </el-table-column>
        <el-table-column label="用户名" prop="username">
        </el-table-column>
        <el-table-column label="id" prop="id" v-if="false">
        </el-table-column>
        <el-table-column label="姓名" prop="name">
        </el-table-column>
        <el-table-column label="总计费" prop="fee" width="120">
        </el-table-column>
        <el-table-column label="用户费" prop="ufee" width="120" v-model="feeform.ufee">
        </el-table-column>
        <el-table-column label="问卷费" prop="qfee" width="120">
        </el-table-column>
        <el-table-column label="题库费" prop="tfee" width="120">
        </el-table-column>
        <el-table-column label="缴费状态" prop="status" width="120">
          <template slot-scope="scope">
            <!--查看租户计费按钮-->
            <el-tag  v-if="scope.row.status === 1" type="success"   effect="plain">已缴费</el-tag>
            <el-tag  v-else="" type="danger"  effect="plain">未缴费</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!--查看租户计费按钮-->
            <el-button v-if="scope.row.status === 0" type="warning"  size="mini" @click="open" round>催缴费</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 7]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      feeform: {
      ufee: 0,
      qfee: 0,
      tfee: 0,
      status: 0
},

      value: true, // 状态
      currentRowIndex: 1,
      // 获取用户列表的参数对象
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        pagesize: 5
      },
      total: 4,
      showtenantList: []
    }
  },
  created () {
    this.gettenantList()
  },
  methods: {
    open () {
     this.$alert('催缴费成功！')
    },
    tableRowClassName ({ row, rowIndex }) {
      row.row_index = rowIndex
    },
    onRowClick (row, event, column) {
      this.currentRowIndex = row.row_index
    },
    // 监听修改用户对话框的关闭事件
    feeDialogClosed () {
      this.$refs.feeFormRef.resetFields()
    },
    // 监听swith用户状态开关的改变
    userStateChanged (userinfo) {
      // 打印状态信息
      console.log(userinfo.mg_state)

      // 这里应该发送http请求
      var len = this.tenantList.length
      for (var i = 0; i < len; i++) {
        var user = this.tenantList[i]
        if (user.id == userinfo.id) {
          user.mg_state = userinfo.mg_state
        }
      }
      gettenantList()
    },
    // 监听pageSize改变的事件
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.gettenantList()
    },
    // 监听页码值改变的事件
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.gettenantList()
    },
     gettenantList () {
      console.log('ces')
      this.$http.get('user/list', {
        params: {
          auth: 2,
          used: 1
        }
      }).then((response) => {
        console.log('ccccc')
        this.showtenantList = []
        console.log(response.data)
        console.log(response.data.data)
        let len = response.data.data.length
        const searchTenantList = []
        for (let i = 0; i < len; i++) {
          const user = response.data.data[i]
          user.createTime = new Date(user.createTime).toLocaleString()
          const feelist = [100, 130, 110, 120]
          user.fee = feelist[(i % 4)]
          switch (user.fee) {
            case 100:user.ufee = 50
                     user.qfee = 30
                     user.tfee = 20
                     user.status = 0
                     break
            case 130:user.ufee = 20
              user.qfee = 60
              user.tfee = 50
              user.status = 1
              break
            case 110:user.ufee = 40
              user.qfee = 20
              user.tfee = 50
              user.status = 0
              break
            case 120:user.ufee = 50
              user.qfee = 40
              user.tfee = 30
              user.status = 1
              break
          }
          if (this.queryInfo.query !== '') {
            if (user.username.indexOf(this.queryInfo.query) !== -1) {
              searchTenantList.push(user)
            }
          } else {
            searchTenantList.push(user)
          }
        }
        console.log(searchTenantList)
        const start = (this.queryInfo.pagenum - 1) * this.queryInfo.pagesize
        len = searchTenantList.length
        for (let i = 0; i < len; i++) {
          if (i >= start && i < start + this.queryInfo.pagesize) {
            const user = searchTenantList[i]
            this.showtenantList.push(user)
          }
        }
        this.total = searchTenantList.length
        console.log(this.showtenantList)
        this.userlist = this.showtenantList
      })
    }
  }
}
</script>

<style lang="less" scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}
.el-dialog{
  height: 600px!important;
}
.box-card {
  width: 100%;
}
</style>
